/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.omn.listazakupow.db.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.omn.listazakupow.db.NoSuchListaZakupowException;
import com.omn.listazakupow.db.model.ListaZakupow;
import com.omn.listazakupow.db.model.impl.ListaZakupowImpl;
import com.omn.listazakupow.db.model.impl.ListaZakupowModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the lista zakupow service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link ListaZakupowUtil} to access the lista zakupow persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see ListaZakupowPersistence
 * @see ListaZakupowUtil
 * @generated
 */
public class ListaZakupowPersistenceImpl extends BasePersistenceImpl<ListaZakupow>
	implements ListaZakupowPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = ListaZakupowImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
			ListaZakupowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
			ListaZakupowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the lista zakupow in the entity cache if it is enabled.
	 *
	 * @param listaZakupow the lista zakupow to cache
	 */
	public void cacheResult(ListaZakupow listaZakupow) {
		EntityCacheUtil.putResult(ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
			ListaZakupowImpl.class, listaZakupow.getPrimaryKey(), listaZakupow);
	}

	/**
	 * Caches the lista zakupows in the entity cache if it is enabled.
	 *
	 * @param listaZakupows the lista zakupows to cache
	 */
	public void cacheResult(List<ListaZakupow> listaZakupows) {
		for (ListaZakupow listaZakupow : listaZakupows) {
			if (EntityCacheUtil.getResult(
						ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
						ListaZakupowImpl.class, listaZakupow.getPrimaryKey(),
						this) == null) {
				cacheResult(listaZakupow);
			}
		}
	}

	/**
	 * Clears the cache for all lista zakupows.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(ListaZakupowImpl.class.getName());
		EntityCacheUtil.clearCache(ListaZakupowImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the lista zakupow.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(ListaZakupow listaZakupow) {
		EntityCacheUtil.removeResult(ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
			ListaZakupowImpl.class, listaZakupow.getPrimaryKey());
	}

	/**
	 * Creates a new lista zakupow with the primary key. Does not add the lista zakupow to the database.
	 *
	 * @param id the primary key for the new lista zakupow
	 * @return the new lista zakupow
	 */
	public ListaZakupow create(long id) {
		ListaZakupow listaZakupow = new ListaZakupowImpl();

		listaZakupow.setNew(true);
		listaZakupow.setPrimaryKey(id);

		return listaZakupow;
	}

	/**
	 * Removes the lista zakupow with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the lista zakupow to remove
	 * @return the lista zakupow that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a lista zakupow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public ListaZakupow remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the lista zakupow with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param id the primary key of the lista zakupow to remove
	 * @return the lista zakupow that was removed
	 * @throws com.omn.listazakupow.db.NoSuchListaZakupowException if a lista zakupow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public ListaZakupow remove(long id)
		throws NoSuchListaZakupowException, SystemException {
		Session session = null;

		try {
			session = openSession();

			ListaZakupow listaZakupow = (ListaZakupow)session.get(ListaZakupowImpl.class,
					new Long(id));

			if (listaZakupow == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
				}

				throw new NoSuchListaZakupowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					id);
			}

			return remove(listaZakupow);
		}
		catch (NoSuchListaZakupowException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected ListaZakupow removeImpl(ListaZakupow listaZakupow)
		throws SystemException {
		listaZakupow = toUnwrappedModel(listaZakupow);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, listaZakupow);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
			ListaZakupowImpl.class, listaZakupow.getPrimaryKey());

		return listaZakupow;
	}

	public ListaZakupow updateImpl(
		com.omn.listazakupow.db.model.ListaZakupow listaZakupow, boolean merge)
		throws SystemException {
		listaZakupow = toUnwrappedModel(listaZakupow);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, listaZakupow, merge);

			listaZakupow.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
			ListaZakupowImpl.class, listaZakupow.getPrimaryKey(), listaZakupow);

		return listaZakupow;
	}

	protected ListaZakupow toUnwrappedModel(ListaZakupow listaZakupow) {
		if (listaZakupow instanceof ListaZakupowImpl) {
			return listaZakupow;
		}

		ListaZakupowImpl listaZakupowImpl = new ListaZakupowImpl();

		listaZakupowImpl.setNew(listaZakupow.isNew());
		listaZakupowImpl.setPrimaryKey(listaZakupow.getPrimaryKey());

		listaZakupowImpl.setId(listaZakupow.getId());
		listaZakupowImpl.setUserId(listaZakupow.getUserId());
		listaZakupowImpl.setDescription(listaZakupow.getDescription());

		return listaZakupowImpl;
	}

	/**
	 * Finds the lista zakupow with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the lista zakupow to find
	 * @return the lista zakupow
	 * @throws com.liferay.portal.NoSuchModelException if a lista zakupow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public ListaZakupow findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the lista zakupow with the primary key or throws a {@link com.omn.listazakupow.db.NoSuchListaZakupowException} if it could not be found.
	 *
	 * @param id the primary key of the lista zakupow to find
	 * @return the lista zakupow
	 * @throws com.omn.listazakupow.db.NoSuchListaZakupowException if a lista zakupow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public ListaZakupow findByPrimaryKey(long id)
		throws NoSuchListaZakupowException, SystemException {
		ListaZakupow listaZakupow = fetchByPrimaryKey(id);

		if (listaZakupow == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
			}

			throw new NoSuchListaZakupowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				id);
		}

		return listaZakupow;
	}

	/**
	 * Finds the lista zakupow with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the lista zakupow to find
	 * @return the lista zakupow, or <code>null</code> if a lista zakupow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public ListaZakupow fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the lista zakupow with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param id the primary key of the lista zakupow to find
	 * @return the lista zakupow, or <code>null</code> if a lista zakupow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public ListaZakupow fetchByPrimaryKey(long id) throws SystemException {
		ListaZakupow listaZakupow = (ListaZakupow)EntityCacheUtil.getResult(ListaZakupowModelImpl.ENTITY_CACHE_ENABLED,
				ListaZakupowImpl.class, id, this);

		if (listaZakupow == null) {
			Session session = null;

			try {
				session = openSession();

				listaZakupow = (ListaZakupow)session.get(ListaZakupowImpl.class,
						new Long(id));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (listaZakupow != null) {
					cacheResult(listaZakupow);
				}

				closeSession(session);
			}
		}

		return listaZakupow;
	}

	/**
	 * Finds all the lista zakupows.
	 *
	 * @return the lista zakupows
	 * @throws SystemException if a system exception occurred
	 */
	public List<ListaZakupow> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the lista zakupows.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of lista zakupows to return
	 * @param end the upper bound of the range of lista zakupows to return (not inclusive)
	 * @return the range of lista zakupows
	 * @throws SystemException if a system exception occurred
	 */
	public List<ListaZakupow> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the lista zakupows.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of lista zakupows to return
	 * @param end the upper bound of the range of lista zakupows to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of lista zakupows
	 * @throws SystemException if a system exception occurred
	 */
	public List<ListaZakupow> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<ListaZakupow> list = (List<ListaZakupow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_LISTAZAKUPOW);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_LISTAZAKUPOW;
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<ListaZakupow>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<ListaZakupow>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the lista zakupows from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (ListaZakupow listaZakupow : findAll()) {
			remove(listaZakupow);
		}
	}

	/**
	 * Counts all the lista zakupows.
	 *
	 * @return the number of lista zakupows
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_LISTAZAKUPOW);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the lista zakupow persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.omn.listazakupow.db.model.ListaZakupow")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<ListaZakupow>> listenersList = new ArrayList<ModelListener<ListaZakupow>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<ListaZakupow>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(ListaZakupowImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = ListaZakupowPersistence.class)
	protected ListaZakupowPersistence listaZakupowPersistence;
	@BeanReference(type = PozycjeListyZakupowPersistence.class)
	protected PozycjeListyZakupowPersistence pozycjeListyZakupowPersistence;
	@BeanReference(type = GrupyTowarowPersistence.class)
	protected GrupyTowarowPersistence grupyTowarowPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_LISTAZAKUPOW = "SELECT listaZakupow FROM ListaZakupow listaZakupow";
	private static final String _SQL_COUNT_LISTAZAKUPOW = "SELECT COUNT(listaZakupow) FROM ListaZakupow listaZakupow";
	private static final String _ORDER_BY_ENTITY_ALIAS = "listaZakupow.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ListaZakupow exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(ListaZakupowPersistenceImpl.class);
}